package com.example.lab.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.Semesters;
import com.example.lab.entity.dto.QueryDto;
import com.example.lab.result.Result;
import com.example.lab.result.ResultCode;
import com.example.lab.service.ISemestersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *@Author: 布莱恩
 *@Date: 2025/6/17 17:01
 *@Description:
 **/
@RestController
@RequestMapping("/api/semesters")
@RequiredArgsConstructor
public class SemesterController {

    private final ISemestersService semesterService;

    // 获取学期列表
    @GetMapping
    public Result<List<Semesters>> getSemesters() {
        List<Semesters> semesters = semesterService.getAllSemesters();
        return Result.success(semesters);
    }

    // 分页获取学期列表
    @GetMapping("/page")
    public Result<Page<Semesters>> getSemestersByPage(QueryDto queryDto) {
        Page<Semesters> page = semesterService.getSemestersByPage(queryDto);
        return Result.success(page);
    }

    // 创建学期
    @PostMapping
    public Result<Integer> createSemester(@RequestBody Semesters semester) {
        Semesters created = semesterService.createSemester(semester);
        return Result.success(created.getSemesterId());
    }

    // 更新学期
    @PutMapping("/{semesterId}")
    public Result<Void> updateSemester(@PathVariable Integer semesterId, @RequestBody Semesters semester) {
        semester.setSemesterId(semesterId);
        boolean success = semesterService.updateSemester(semester);
        return success ? Result.success() : Result.fail(ResultCode.FAIL.getCode(), "学期更新失败");
    }

    // 删除学期
    @DeleteMapping("/{semesterId}")
    public Result<Void> deleteSemester(@PathVariable Integer semesterId) {
        boolean success = semesterService.deleteSemester(semesterId);
        return success ? Result.success() : Result.fail(ResultCode.FAIL.getCode(), "学期删除失败");
    }

    // 获取学期详情
    @GetMapping("/{semesterId}")
    public Result<Semesters> getSemester(@PathVariable Integer semesterId) {
        Semesters semester = semesterService.getSemesterById(semesterId);
        return semester != null ? Result.success(semester) : Result.fail(ResultCode.NOT_FOUND.getCode(), "学期不存在");
    }
}
